package com.bing.habit.service.impl;

import com.bing.habit.mapper.IconCategoryDAO;
import com.bing.habit.entity.HabitIcon;
import com.bing.habit.entity.IconCategory;
import com.bing.habit.service.HabitIconService;
import com.bing.habit.service.IconCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IconCategoryServiceImpl implements IconCategoryService {

    @Autowired
    IconCategoryDAO iconCategoryDAO;
    @Autowired
    HabitIconService habitIconService;

    @Override
    public void addOrUpdate(IconCategory iconCategory) {
        if (iconCategory.getId() == 0) {//不存在新增
            iconCategoryDAO.insert(iconCategory);
        } else {//修改
            iconCategoryDAO.updateById(iconCategory);
        }
    }

    @Override
    public List<IconCategory> list() {
        //图标分类列表
        List<IconCategory> iconCategories = iconCategoryDAO.selectList(null);
        //根据分类查询 对应图标列表
        for (IconCategory iconCategory : iconCategories) {
            List<HabitIcon> habitIcons = habitIconService.listByCid(iconCategory.getId());
            iconCategory.setHabitIcons(habitIcons);
        }
        return iconCategories;
    }

    @Override
    public void delete(int id) {
        iconCategoryDAO.deleteById(id);
    }

}
